﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuildHelper.Work.GUI;
using System.Diagnostics;
using ProgNetComponentsDx.Browsers;
using ProgNetComponentsDx.Options;

namespace BuildHelper.Actions
{
    public class GuiBuildAction : ProcessActionBase
    {
        public event ProgressHandler Progress;

        public GuiBuildAction()
        {
            Errors = new List<string>();
        }
        public bool ShowAlert
        {
            get;
            set;
        }
        public string Brancz
        {
            get;
            set;
        }
        public GuiDataSet DataSet
        {
            get;
            set;
        }
        public string Serwer
        {
            get;
            set;
        }
        public string Env
        {
            get;
            set;
        }
        public bool Uruchom
        {
            get;
            set;
        }
        public bool ZatrzymajNaBledzie
        {
            get;
            set;
        }
        public string SingleSystem
        {
            get;
            set;
        }
        public bool Clean
        {
            get;
            set;
        }
        public string Konfig
        {
            get;
            set;
        }
        private int? ServerId
        {
            get
            {
                if (Serwer.IsNotNull())
                {
                    return Serwer.AsInt();
                }
                object defServer = OptionCache.GetOptionValue(Init.InitFormOptionNames.Rozne_Group.StatusBar.DefServer);
                if (defServer.AsInt() > 0)
                    return defServer.AsInt();
                return null;
            }
        }

        public List<string> Errors
        {
            get;
            private set;
        }
        
        public override bool ValidateAll()
        {
            return DataSet!=null;
        }

        int buildProj = 0;
        int buildProjAll = 0;
        int allProjs = 0;
        List<BuildStm> systemy = new List<BuildStm>();
        BuildStm curStm = null;
        string projekt = "";

        private void SetCurStm(BuildStm s)
        {
            if (curStm != s)
            {
                curStm = s;
                buildProj = 0;
                projekt = "";
                SetProgressTest();
            }
        }
        private void SetProgressTest()
        {
            if (systemy != null && curStm != null)
            {
                int cSys = systemy.IndexOf(curStm) + 1;
                int cProj = Math.Max(1, buildProj);
                string p = "";
                if (buildProj > 0)
                    p = projekt;
                string text = "System: " + cSys + "/" + systemy.Count + " (" + curStm.System + ")";

                if (systemy.Count > 1)
                {
                    int ba = Math.Max(buildProjAll, 1);
                    text += Constants.NewLineLinux + "Projekty: " + ba + "/" + allProjs;
                }
                text += Constants.NewLineLinux + "Obecny Projekt: " + cProj + "/" + curStm.Projects +
                     Constants.NewLineLinux + p;
                if (Progress != null)
                    Progress(text);

                text = "Budowanie GUI [" + Brancz + "]" + Constants.NewLineLinux + text;
                FormMain.UpdateAlertText(AlertId, text);
            }
        }

        protected override void _Start()
        {
            Errors.Clear();
            DataSet = DataSet.Copy() as GuiDataSet;
            systemy.Clear();
            curStm = null;
            projekt = "";
            buildProj = 0;
            buildProjAll = 0;
            allProjs = 0;

            if (ShowAlert)
            {
                FormMain.AlertInfo a = new FormMain.AlertInfo(true, AlertId);
                FormMain.ShowAlert("Budowanie GUI [" + Brancz + "]", Properties.Resources.burn_gui24, a);
            }
            BuildStm[] stms = BudowanieGuiTools.Buduj(DataSet, Env, TheProcess, Uruchom, Clean, Konfig, Env, ServerId);
            systemy.AddRange(stms);
            foreach (BuildStm s in stms)
                allProjs += s.Projects;
            if (stms.Length > 0)
                SetCurStm(stms[0]);
        }

        protected override void _Cancel()
        {
            
        }

        protected override void _OnDataReceived(string str)
        {
            
        }
    }
    
}
